<?php

require '../vendor/autoload.php';
// use ShugaChara\Music\API;
use Metowolf\Meting;

class Handler
{
    // /**
    //      * 网易云
    //      */
    //     const SITE_NETEASE = 'netease';
    //     /**
    //      * 腾讯QQ
    //      */
    //     const SITE_TENCENT = 'tencent';
    //     /**
    //      * 虾米
    //      */
    //     const SITE_XIAMI = 'xiami';

    //     /**
    //      * 酷狗
    //      */
    //     const SITE_KUGOU = 'kugou';
    //     /**
    //      * 百度
    //      */
    //     const SITE_BAIDU = 'baidu';
    //     kuwo
    public function searchBy($keyword, $site = 'kuwo')
    {
        try {
            $meting = $this->getMeting($site);
            $musics = $meting->format()->search($keyword, ['page' => 1, 'limit' => 3]);
            // print_r($musics);
            // exit;
            $musics = json_decode($musics, 1);
            if (count($musics)) {
                $musics[0]['meting'] = $meting;
                return $musics[0];
            }
        } catch (\Exception $e) {
            return false;
        }
    }
    public function playBy($id, $site = 'kuwo', $meting = null)
    {
        try {
            $meting = $meting ?? $this->getMeting($site);
            $music = $meting->format()->url($id);
            // print_r(gettype($music));
            // print_r($site);
            $music = json_decode($music, 1);
            // print_r(($music));
            // exit;
            return $music['url'];
        } catch (\Exception $e) {
            return false;
        }
    }
    public function picBy($id, $site = 'kuwo', $meting = null)
    {
        try {
            $meting = $meting ?? $this->getMeting($site);
            $music = $meting->format()->pic($id);
            $music = json_decode($music, 1);
            // print_r($music);
            return $music['url'];
            // return $music;
        } catch (\Exception $e) {
            return false;
        }
    }
    //['kugou', 'netease', 'tencent', 'xiami', 'kuwo', 'baidu']
    public function nextSearch($keyword, $sites = ['kugou', 'kuwo', 'tencent', 'netease'])
    {
        foreach ($sites as $site) {
            $music = $this->searchBy($keyword, $site);
            if ($music) {
                $music['link'] = $this->playBy($music['id'], $music['source'], $music['meting']);
                if ($music['link']) {
                    try {
                        $music['pic'] = $this->picBy($music['id'], $music['source'], $music['meting']);
                    } catch (\Exception $e) {
                        // 没有图
                    }
                    break;
                } else {
                    continue;
                }
            }
        }
        unset($music['meting']);
        return $music;

    }
    public function search($keyword)
    {
        $music = $this->nextSearch($keyword);
        echo json_encode($music);
    }
    public function searchChannel($keyword, $site = 'kuwo')
    {
        $music = $this->nextSearch($keyword, [$site]);
        echo json_encode($music);
    }
    public function getMeting(string $platform): Meting
    {
        return new Meting($platform);
    }
    public function formatAll(array $songs, string $keyword): array
    {
        foreach ($songs as $key => &$song) {
            $song = $this->format($song, $keyword);
            array_unshift($song, "<fg=cyan>$key</>");
        }

        unset($song);

        return $songs;
    }

    public function format(array $song, string $keyword): array
    {
        foreach ($this->hideFields as $hideField) {
            unset($song[$hideField]);
        }

        $song['name'] = str_replace($keyword, "<fg=red;options=bold>$keyword</>", $song['name']);
        $song['album'] = str_replace($keyword, "<fg=red;options=bold>$keyword</>", $song['album']);
        $song['artist'] = implode(',', $song['artist']);
        $song['artist'] = str_replace($keyword, "<fg=red;options=bold>$keyword</>", $song['artist']);
        $song['size'] = '<fg=yellow>' . sprintf('%.1f', $song['size'] / 1048576) . 'M</>';

        return $song;
    }
}

header('Content-Type: application/json; charset=utf-8');
$router = include "router.inc.php";
$router->execute();
// $router = (new CRouter('router.inc.php', true));
// $router
//     ->get('/searchBy/:keyword/:site', array(new Handler, 'searchChannel'))
//     ->get('/search/:keyword', array(new Handler, 'search'))
//     ->execute();
